package com.edu.web.controller.info;

import com.edu.framework.websocket.service.HighlighsService;
import com.edu.system.domain.TNotice;
import com.edu.system.domain.dto.HighlightsDTO;
import com.edu.system.domain.req.PublishDTO;
import com.edu.system.service.ITPublishClassService;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.edu.common.annotation.Log;
import com.edu.common.core.controller.BaseController;
import com.edu.common.core.domain.AjaxResult;
import com.edu.common.enums.BusinessType;
import com.edu.system.domain.THighlights;
import com.edu.system.service.ITHighlightsService;
import com.edu.common.utils.poi.ExcelUtil;
import com.edu.common.core.page.TableDataInfo;

/**
 * 校园风采Controller
 * 
 * @author xxxx
 * @date 2023-12-27
 */
@Slf4j
@RestController
@RequestMapping("/system/highlights")
public class THighlightsController extends BaseController
{
    @Autowired
    private ITHighlightsService tHighlightsService;
    @Resource
    private ITPublishClassService publishClassService;
    @Resource
    private HighlighsService wsHighlighsService;

    // 1通知 2校园风采
    public static Integer publishClass_type=2;

    /**
     * 查询校园风采列表
     */
    @PreAuthorize("@ss.hasPermi('system:highlights:list')")
    @GetMapping("/list")
    public TableDataInfo list(THighlights tHighlights)
    {
        startPage();
        List<THighlights> list = tHighlightsService.selectTHighlightsList(tHighlights);
        return getDataTable(list);
    }

    /**
     * 导出校园风采列表
     */
    @PreAuthorize("@ss.hasPermi('system:highlights:export')")
    @Log(title = "校园风采", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, THighlights tHighlights)
    {
        List<THighlights> list = tHighlightsService.selectTHighlightsList(tHighlights);
        ExcelUtil<THighlights> util = new ExcelUtil<THighlights>(THighlights.class);
        util.exportExcel(response, list, "校园风采数据");
    }

    /**
     * 获取校园风采详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:highlights:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tHighlightsService.selectTHighlightsById(id));
    }

    /**
     * 新增校园风采
     */
    @PreAuthorize("@ss.hasPermi('system:highlights:add')")
    @Log(title = "校园风采", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HighlightsDTO tHighlights)
    {
        int i = tHighlightsService.insertTHighlights(tHighlights);
        return toAjax(i);
    }

    /**
     * 修改校园风采
     */
    @PreAuthorize("@ss.hasPermi('system:highlights:edit')")
    @Log(title = "校园风采", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HighlightsDTO tHighlights)
    {
        int i = tHighlightsService.updateTHighlights(tHighlights);

        this.doPushMsg(Collections.singletonList(tHighlights.getId()));
        return toAjax(i);
    }

    /**
     * 批量发布
     */
    @PreAuthorize("@ss.hasPermi('system:highlights:publish')")
    @PostMapping(value = "/publish")
    public AjaxResult publish(@RequestBody PublishDTO req)
    {
        req.setPublishType(2);
        publishClassService.savePublishClass(req);

        this.doPushMsg(req.getIds());
        return success();
    }

    /**
     * 删除校园风采
     */
    @PreAuthorize("@ss.hasPermi('system:highlights:remove')")
    @Log(title = "校园风采", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int i = tHighlightsService.deleteTHighlightsByIds(ids);

        this.doPushMsg(Arrays.asList(ids));
        return toAjax(i);
    }

    private void doPushMsg(List<Long> ids) {
        List<Long> roomIds = publishClassService.queryClassIdsByTids(ids, 2);
        CompletableFuture.runAsync(() -> {
            wsHighlighsService.pushMsg(roomIds);
        });
    }

    /**
     * 下发通知
     * 异步
     * @param ids
     */
    private void pushMsg(List<Long> ids, boolean isCheckDouble) {
        List<Long> before = publishClassService.queryClassIdsByTids(ids, publishClass_type);
        CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                log.error("睡眠异常");
            }
            if (isCheckDouble){
                List<Long> after = publishClassService.queryClassIdsByTids(ids, publishClass_type);
                before.addAll(after);
            }
            wsHighlighsService.pushMsg(before);
        });
    }
}
